package com.wyh.spring.demo.controller;

import com.wyh.spring.demo.model.UserInfo;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created with IntelliJ IDEA.
 * Description:返回
 * User: 王圆豪
 * Date: 2025-06-30
 * Time: 17:08
 */

@RequestMapping("response")
@Controller
public class RespController {
    /**
     * 返回页面
     * 注意点：1.@Controller
     *       2.页面所在位置前要加 ’/‘
     */
    @RequestMapping("r1")
    public String returnPage(){
        return "/index.html";
    }

    /**
     * 返回数据
     * 注意点：@ResponseBody
     */
    @RequestMapping("r2")
    @ResponseBody
    public String returnData(){
        return "返回数据";
    }

    /**
     * 返回的数据是html标签
     */
    @RequestMapping("r3")
    @ResponseBody
    public String returnHtml(){
        return "<h1>returnHtml</h1>";
    }

    /**
     * 如何返回html标签并且类型为纯文本类型
     */
    @RequestMapping(value = "r4", produces = "text/plain")
    @ResponseBody
    public String returnHtml2(){
        return "<h1>returnHtml</h1>";
    }

    /**
     *返回Json
     */
    @RequestMapping("r5")
    @ResponseBody
    public UserInfo returnJson(){
        UserInfo userInfo = new UserInfo("wyh",1,1);
        return userInfo;
    }

    /**
     * 修改状态码
     */
    @RequestMapping("r6")
    @ResponseBody
    public UserInfo setStatus(HttpServletResponse response){
        response.setStatus(401);
        UserInfo userInfo = new UserInfo("wyh",1,1);
        return userInfo;
    }

    /**
     * 设置header
     */
    @RequestMapping("r7")
    @ResponseBody
    public String setHeader(HttpServletResponse response){
        response.setHeader("myHeader", "myHeader");
        return "设置成功";
    }
}
